package cn.scxy.bookcity.service.impl;

import cn.scxy.bookcity.ex.ServiceException;
import cn.scxy.bookcity.mapper.NoticeMapper;
import cn.scxy.bookcity.pojo.entity.Notice;
import cn.scxy.bookcity.pojo.vo.ActivityListVO;
import cn.scxy.bookcity.service.INoticeService;
import cn.scxy.bookcity.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
@Slf4j
public class NoticeServiceImpl implements INoticeService {
    @Autowired
    private NoticeMapper mapper;
    @Override
    public List<Notice> list() {
        List<Notice>  n=mapper.list();
        return n;
    }
    @Override
    public void setEnable1(Integer id) {
        updateEnableById1(id, 1);
    }

    @Override
    public void setDisable1(Integer id) {
        updateEnableById1(id, 0);
    }

    @Override
    public void update(Notice notice) {
        mapper.updateNotice(notice);
    }

    @Override
    public void delete(Long id) {
        mapper.delete(id);
    }

    @Override
    public void insetNotice(Notice notice) {
        mapper.insetNotice(notice);
    }

    private void updateEnableById1(Integer id, Integer enable) {
        String[] tips = {"显示", "隐藏"};
        log.debug("开始处理【{}公告】的业务，参数：{}", id);
        // 调用Mapper对象的getDetailsById()方法执行查询
        Notice noriceById = mapper.getNoriceById(id);
        // 判断查询结果是否为null
        if (noriceById == null) {
            String message ="修改失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        // 判断查询结果中的enable是否为1
        if (noriceById.getState().equals(enable)) {
            String message ="修改失败，当前类别已经处于 此状态！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        // 准备执行更新
        Notice notice=new Notice();
        notice.setId(id);
        notice.setState(enable);
        int rows = mapper.updateNotice(notice);
        if (rows != 1) {
            String message ="类别失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }

    }
}
